//
//  LSZPrayerWitnessVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/12/15.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZPrayerWitnessVC.h"

@interface LSZPrayerWitnessVC ()

@end

@implementation LSZPrayerWitnessVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleString;

    self.textViewControl = [[UITextView alloc]init];
    self.textViewControl.textColor = titleColorLSZ;
    self.textViewControl.textAlignment = NSTextAlignmentLeft;
    // 设置自动纠错方式
    self.textViewControl.autocorrectionType = UITextAutocorrectionTypeNo;
    self.textViewControl.returnKeyType = UIReturnKeyDefault;
    // 设置是否可以拖动
    self.textViewControl.scrollEnabled = YES;
    // 设置代理
    self.textViewControl.delegate = self;
    //自适应高度
    self.textViewControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    // 自定义文本框字数统计
    self.count = [[UILabel alloc] init];
    self.count.text = @"0/1000";
    self.count.textAlignment = NSTextAlignmentRight;
    self.count.backgroundColor = [UIColor clearColor];
    self.count.textColor = [UIColor redColor];
    self.count.enabled = NO;
    [self.view addSubview:self.count];
    [self.view addSubview:self.textViewControl];

    if (ScreenWidth == 320) {
        //5
        self.textViewControl.font = FONT_(13.5);
        self.count.font = FONT_(12.5);

        self.textViewControl.frame = CGRectMake( 5, NAVIGATION_BAR_HEIGHT+5, ScreenWidth-10, 200);
        self.count.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 70, CGRectGetMaxY(self.textViewControl.frame) +5, 60, 16);
    }else if(ScreenWidth == 375){
        // 6 7
        self.textViewControl.font = FONT_(14);
        self.count.font = FONT_(13);
        self.textViewControl.frame = CGRectMake( 5, NAVIGATION_BAR_HEIGHT+5, ScreenWidth-10, 250);
        self.count.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 70, CGRectGetMaxY(self.textViewControl.frame) +5, 60, 18);
    }else {
        //plus
        self.textViewControl.font = FONT_(14.5);
        self.count.font = FONT_(13.5);
        self.textViewControl.frame = CGRectMake( 5, NAVIGATION_BAR_HEIGHT+5, ScreenWidth-10, 300);
        self.count.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 70, CGRectGetMaxY(self.textViewControl.frame) +5, 60, 20);
    }

    //完成按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 40, 40);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     rightBtn.titleLabel.font = FONT_16;
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

}
-(void)rightBtnAction
{
    NSString * str = [NSString stringWithFormat:@"%@",self.textViewControl.text];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:str forKey:@"param"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil userInfo:dic];
     [self.navigationController popViewControllerAnimated:YES];
}
/**
 将要开始编辑
 @param textView textView
 @return BOOL
 */
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}
/**
 开始编辑
 @param textView textView
 */
- (void)textViewDidBeginEditing:(UITextView *)textView
{
}
/**
 将要结束编辑
 @param textView textView
 @return BOOL
 */
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location < 1000){
        return  YES;
    } else  if ([textView.text isEqualToString:@"\n"]) {
             //这里写按了ReturnKey 按钮后的代码
            return NO;
    }
     if (textView.text.length >=1000) {
         [SVProgressHUD showErrorWithStatus:@"注意内容不能超过1000个!"];
         [SVProgressHUD dismissWithDelay:1];
         return NO;
    }
     return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
    self.count.text = [NSString stringWithFormat:@"%ld/1000",(long)textView.text.length];
}
@end
